define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Productmanager_brand,$timeout,GoPage,$window,Auth,GoDialog) {
		
		
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//得到当前用户身份。1:开发商，2:品牌代理商，3:代理商/分仓	4:经销商
			$scope.user_type = Auth.check_user_type();
			if($scope.user_type==1||$scope.user_type==2||$scope.user_type==4){
				$scope.isShowAddBtn = true;
			}else{
				$scope.isShowAddBtn = false;
			}
			/*
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) {
				lan = 20;
			}
			var all = $window.innerHeight;
			$scope.myObj = {
				"height": all - 44 - addHeight - lan + "px"
			};
			*/
			//动态设置高度
			 //GoPage.init_top_bottom($scope);
			
			$scope.data_init();
		}, false);
		
		//初始化页面  1：申请中   2：使用中  3：未通过
		$scope.data_init = function(){
			Productmanager_brand.brand_list(1, function(data) {
				if(data){
					$scope.brands = data.brand;
				}
			})
		}
		/*=============删除品牌================*/
		$scope.delete_brand = function(id) {
			Productmanager_brand.delete_brand(id, function(data) {
				if(data.success) {
					GoDialog.toast("删除成功！");
					$scope.delete_brandItem($scope.brands, id);
				} else {
					GoDialog.toast(data.msg);
				}
			})
		};
		$scope.delete_brandItem = function(datas, id) {
			var index = -1;
			for(var i = 0; i < datas.length; i++) {
				if(datas[i].id == id) {
					index = i;
					break; //一找到就退出循环
				}
			}
			if(index != -1) { //说明找到对应的商品
				datas.splice(index, 1);
			}
		};
		/*=============删除品牌================*/
		
		//编辑
		$scope.onshow_productmanager_brand_edit = function(brand_id) {
			if($scope.user_type == 1 || $scope.user_type == 2) {
				GoPage.go_states("productmanager_brand_edit",{"id":brand_id});
			} else if($scope.user_type == 4) {
				GoPage.go_states("productmanager_brand_agent_edit",{"id":brand_id});
			}
		};
		
		//根据按钮的类型，选择对应的事件
		$scope.switchEvent = function(buttonId, brand_id, errorMsg, confirmMsg) {
			if(errorMsg){//有错误提示，就不触发以下事件
				GoDialog.alert("操作失败",errorMsg);
				return;
			}
			switch(buttonId) {
				case 1://编辑
					$scope.onshow_productmanager_brand_edit(brand_id);
					break;
				case 2://删除
					GoDialog.confirm("删除品牌",confirmMsg,function(){
						$scope.delete_brand(brand_id);
					})
					break;
			}	
		}
		
		//品牌详情
		$scope.onshow_detail = function(brand_id){
			GoPage.go_states("branddetail",{"id":brand_id});
		}
		
		/*********************上下拉刷新********************/
		$scope.nPage = 1;
		$scope.isLoad = false;
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Productmanager_brand.brand_list(1,function(data) {
				if(data != null) {
					$scope.brands = data.brand;
				} else {
					$scope.pending = new Object();
				}
				$scope.nPage = 1;
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		}
		
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			Productmanager_brand.brand_list($scope.nPage + 1,function(data) {
				if(data != null) {
					if($scope.brands != undefined) {
						$scope.brands = $scope.brands.concat(data.brand);
						$scope.nPage += 1;
					} else {
						$scope.brands = data.brand;
					}
				}
			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		}
		/************上下拉刷新**************/
			
	}

	controller.$inject = ['$scope', 'Productmanager_brand','$timeout','GoPage','$window','Auth','GoDialog'];
	
	return app.registerController("productmanager_brandctrl",controller);
});